﻿#region

using System.Collections.Generic;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using Pob.ScreenManagement;

#endregion

namespace Pob.GameObjects
{
    internal class RoundManager : GameComponent
    {
        #region Fields

        private readonly List<GameComponent> _drawStrings;

        private readonly Player _playerOne;

        private readonly Player _playerTwo;

        private readonly List<GameComponent> _players;

        private readonly DrawString _scoreString;

        private readonly ScreenBase _screen;

        private readonly Settings _settings;

        private readonly DrawString _timeString;

        private double _roundTime;

        #endregion

        #region Constructors and Destructors

        public RoundManager(Game game, ScreenBase screen)
            : base(game)
        {
            _settings = SettingsManager.CurrentInstance.Settings;

            _playerOne = new Player(game, _settings.PlayerOne.Name, _settings.PlayerOne.KeyUp, _settings.PlayerOne.KeyDown, _settings.PlayerOne.KeyPowerUp, Side.Left);
            _playerTwo = new Player(game, _settings.PlayerTwo.Name, _settings.PlayerTwo.KeyUp, _settings.PlayerTwo.KeyDown, _settings.PlayerTwo.KeyPowerUp, Side.Right);

            _players = new List<GameComponent>
                {
                    _playerOne, 
                    _playerTwo
                };

            _scoreString = new DrawString(game, GameContent.FontScoreboardPointsTime, _playerOne.Score + ":" + _playerTwo.Score, new Vector2(_settings.Width / 2f, 55), Color.White, drawStringMode: DrawStringMode.Extended);

            _timeString = new DrawString(game, GameContent.FontScoreboardPointsTime, "0:00", new Vector2(_settings.Width / 2f, 35), Color.White, drawStringMode: DrawStringMode.Extended);

            _drawStrings = new List<GameComponent>
                {
                    new DrawString(game, GameContent.FontScoreboardPlayer, _playerOne.Name, new Vector2(30, 40), Color.White, originMode: OringeMode.TopLeft), 
                    new DrawString(game, GameContent.FontScoreboardPlayer, _playerTwo.Name, new Vector2(_settings.Width - 30, 40), Color.White, originMode: OringeMode.TopRight), 
                };

            _screen = screen;

            _roundTime = -1;
        }

        #endregion

        #region Public Properties

        public Player Player1
        {
            get
            {
                return _playerOne;
            }
        }

        public Player Player2
        {
            get
            {
                return _playerTwo;
            }
        }

        public double RoundTime
        {
            get
            {
                return _roundTime;
            }
        }

        #endregion

        #region Public Methods and Operators

        public void Activate()
        {
            _screen.AddToMyComponents(_playerOne);
            _screen.AddToMyComponents(_playerTwo);
            _screen.AddToMyComponents(_scoreString);
            _screen.AddToMyComponents(_timeString);
            _screen.AddRangeToMyComponents(_drawStrings);
        }

        public void StartNewRound()
        {
            _roundTime = -1;

            _scoreString.Text = _playerOne.Score + ":" + _playerTwo.Score;
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState kbState = Keyboard.GetState();

            if (_roundTime == -1)
            {
                if (kbState.IsKeyDown(Keys.Space))
                {
                    _roundTime = 0;
                }
            }
            else
            {
                _roundTime += gameTime.ElapsedGameTime.TotalSeconds;

                _timeString.Text = (int)(_roundTime / 60) + ":" + string.Format("{0:00}", ((int)_roundTime) % 60);

                foreach (var player in _players.Cast<Player>())
                {
                    if (player.StoredPowerUp != null && kbState.IsKeyDown(player.KeyPowerUp))
                    {
                        player.ActivatePowerUp(gameTime);
                    }
                }
            }

            base.Update(gameTime);
        }

        #endregion
    }
}